package com.leapp.sk.ui.adapter

import android.content.Context
import android.view.View
import com.example.mvp.adapter.BaseLibMultipleViewAdapter
import com.example.mvp.binder.BaseDataBinder
import com.example.mvp.delegate.BaseAdapterDelegate
import com.leapp.sk.R
import com.leapp.sk.bean.CompleteTaskEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.viewdelegate.adapter.task.ImageTaskAdapterDelegate
import com.leapp.sk.viewdelegate.adapter.task.VideoTaskAdapterDelegate
import com.leapp.sk.viewdelegate.adapter.task.VoiceTaskAdapterDelegate

/**
 * 完成作业适配器
 * @author  chaohaipeng  on 2018/8/3.
 */
class CompleteTaskAdapter (context: Context, mlist: MutableList<CompleteTaskEntity>,isdetial:Boolean) : BaseLibMultipleViewAdapter<CompleteTaskEntity>(context, mlist){
    /**
     * 数据
     */
    val mList = mlist
    /**
     * 上下文
     */
    val mContext = context
    /**
     * 是详情
     */
    val isDetial=isdetial

    override fun bindEvenListener(viewDelegate: BaseAdapterDelegate?, position: Int) {
        if (!isDetial){
            val get = mList.get(position)
            if (viewDelegate is VoiceTaskAdapterDelegate){
                viewDelegate.setOnClickListener(View.OnClickListener {
                    if (mList.contains(get)){
                        mList.remove(get)
                        val file = get.file
                        if (file!=null&&file.exists()){
                            file.delete()
                        }
                    }
                    notifyDataSetChanged()
                }, R.id.iv_atv_delete_voice)
            }else if (viewDelegate is ImageTaskAdapterDelegate){
                viewDelegate.setOnClickListener(View.OnClickListener {
                    if (mList.contains(get)){
                        mList.remove(get)
                    }
                    notifyDataSetChanged()
                }, R.id.iv_ati_delete_btn)
            }else if (viewDelegate is VideoTaskAdapterDelegate){
                viewDelegate.setOnClickListener(View.OnClickListener {
                    if (mList.contains(get)){
                        mList.remove(get)
                        val file = get.file
                        if (file!=null&&file.exists()){
                            file.delete()
                        }
                    }
                    notifyDataSetChanged()
                }, R.id.iv_atv_video_delete_btn)
            }
        }

    }

    override fun notifyView(viewDelegate: BaseAdapterDelegate?, position: Int) {
        val get = mList.get(position)
        if (viewDelegate is VoiceTaskAdapterDelegate){
            viewDelegate.setData(get,isDetial)
        }else if (viewDelegate is ImageTaskAdapterDelegate){
            viewDelegate.setData(mContext,get.url,isDetial)

        }else if (viewDelegate is VideoTaskAdapterDelegate){
            viewDelegate.setData(mContext,get,isDetial)
        }
    }

    override fun getDelegate(position: Int): BaseAdapterDelegate {
        val itemViewType = getItemViewType(position)
        when (itemViewType) {
            1->{
                return VoiceTaskAdapterDelegate::class.java?.newInstance()
            }
           2->{
                return ImageTaskAdapterDelegate::class.java?.newInstance()
            }
            3->{
                return VideoTaskAdapterDelegate::class.java?.newInstance()
            }
            else ->{
                return VoiceTaskAdapterDelegate::class.java?.newInstance()
            }
        }

    }

    override fun getDataBinder(position: Int): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun getItemData(position: Int): Any {
      return  mList.get(position)
    }

    override fun getItemViewType(position: Int): Int {
        val get = mList.get(position)
         return get.type
    }

}